package com.it.cloud.generator.result;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.it.cloud.common.A;
import com.it.cloud.common.R;
import com.it.cloud.common.Req;
import com.it.cloud.dao.BaseDao;
import com.it.cloud.dao.page.PageBean;
import lombok.AllArgsConstructor;
import ${entityImport};
import ${parentEntityImport};

@RestController
@AllArgsConstructor
@Api(tags={"${name}"})
public class ${name}Controller {

	private BaseDao baseDao;
	
	@PostMapping("${mapping}/add")
	@ApiOperation(value = "新增")
	@ApiOperationSupport(order = 1)
	public R<?> add(Long pid, ${name} obj) {
		${parent} parent = baseDao.findById(${parent}.class, pid);
		A.isInDatabase(parent!=null, "主表");
		obj.set${parent}(parent);
		baseDao.save(obj);
		return R.success();
	}
	
	@PostMapping("${mapping}/del")
	@ApiOperation(value = "删除")
	@ApiOperationSupport(order = 2)
	public R<?> del(String ids) {
		List<Long> idList = Req.getIdsByStr(ids);
		baseDao.delByIds(${name}.class, idList);
		return R.success();
	}
	
	@PostMapping("${mapping}/update")
	@ApiOperation(value = "修改")
	@ApiOperationSupport(order = 3)
	public R<?> update(${name} obj) {
		${name} oldObj = baseDao.findById(${name}.class, obj.getId());
<#list editProNames as name>
		oldObj.set${name?cap_first}(obj.get${name?cap_first}());
</#list>
		baseDao.update(oldObj);
		return R.success();
	}
	
	@PostMapping("${mapping}/batch")
	@ApiOperation(value = "批量修改")
	@ApiOperationSupport(order = 4)
	public R<?> batch(
		String ids,
<#list batchProNames as name>
		Boolean ${name}Ck,
</#list>
		${name} obj
		) {
		List<Long> idList = Req.getIdsByStr(ids);
		for(Long id:idList) {
			${name} oldObj = baseDao.findById(${name}.class, id);
<#list batchProNames as name>
			if(${name}Ck){
				oldObj.set${name?cap_first}(obj.get${name?cap_first}());
			}
</#list>
			baseDao.update(oldObj);
		}
		return R.success();
	}
	
	@GetMapping("${mapping}/info")
	@ApiOperation(value = "查询详情")
	@ApiOperationSupport(order = 5)
	public R<${name}> info(Long id) {
		${name} obj = baseDao.findById(${name}.class, id);
		return R.success(obj);
	}
	
	@GetMapping("${mapping}/list")
	@ApiOperation(value = "查询全部")
	@ApiOperationSupport(order = 6)
	public R<List<${name}>> list(Long pid) {
		${parent} parent = baseDao.findById(${parent}.class, pid);
		A.isInDatabase(parent!=null, "主表");
		List<${name}> list = baseDao.find(${name}.class, ${name}.${parentStaticName}+"=?0", new Object[] {parent}, ${name}.ID);
		return R.success(list);
	}
	
	@GetMapping("${mapping}/page")
	@ApiOperation(value = "查询分页")
	@ApiOperationSupport(order = 7)
	public R<PageBean<${name}>> page(Long pid, Integer page, Integer pageSize) {
		${parent} parent = baseDao.findById(${parent}.class, pid);
		A.isInDatabase(parent!=null, "主表");
		
		StringBuffer sb = new StringBuffer("1=1");
		List<Object> paramList = new ArrayList<>();
		Integer index = 0;
		
		{
			sb.append(" and "+${name}.${parentStaticName}+"=?"+index);
			paramList.add(parent);
			index++;
		}
		
		/*
		String searchStr = Req.getString("searchStr");
		if(searchStr!=null&&!searchStr.equals("")) {
			sb.append(" and concat(" + ${name}.NAME+") like ?"+i);
			paramList.add("%"+searchStr+"%");
			i++;
		}
		*/
		String where = sb.toString();
		Object[] params = paramList.toArray();
		PageBean<${name}> pb = baseDao.page(${name}.class, where, params, page, pageSize, ${name}.ID);
		return R.success(pb);
	}
	
	@GetMapping("${mapping}/parent")
	@ApiOperation(value = "查询主表")
	@ApiOperationSupport(order = 7)
	public R<List<${parent}>> parent() {
		List<${parent}> list = baseDao.findAll(${parent}.class, ${parent}.ID);
		return R.success(list);
	}
}
